/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-11-07 19:06:51
 * @LastEditTime: 2023-01-29 14:45:37
 */
import {
  SET_REPORT_LIST,
  ADD_REPORT_LIST,
  DELETE_REPORT_LIST,
  SET_SELECT_KEY,
  EDIT_REPORT_LIST,
  SET_SELECT_FOND,
  SET_SELECT_AT,
  SET_SELECT_LEVEL
} from '../constants';
import { delItemByIdArray, editItemById, insertItem } from '@/utils/list';

export const ReportTemplateReducer = (state, action) => {
  switch (action.type) {
    // 表格加载loading
    case SET_SELECT_FOND:
      return {
        ...state,
        selectFond: action.data
      };

    // 设置选中的档案类型
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };

    /**
     * @description: 设置选中的层级
     * @param {object} data 选中的层级
     */
    case SET_SELECT_LEVEL:
      console.log('selectLevel reducer', action.data);
      return {
        ...state,
        selectLevel: action.data
      };

    case SET_SELECT_KEY: {
      return {
        ...state,
        selectItems: action.data
      };
    }

    case SET_REPORT_LIST: {
      return {
        ...state,
        reportList: action.data
      };
    }

    case ADD_REPORT_LIST: {
      return {
        ...state,
        reportList: insertItem(action.data, state.reportList)
      };
    }
    /**
     * @description: 编辑规则配置
     */
    case EDIT_REPORT_LIST: {
      const { id, data } = action;
      console.log(action);
      return {
        ...state,
        reportList: editItemById(id, data, state.reportList)
      };
    }

    case DELETE_REPORT_LIST: {
      return {
        ...state,
        reportList: delItemByIdArray(action.ids, state.reportList),
        selectItems: []
      };
    }

    default:
      console.log('untouched', action);
      return state;
  }
};
